package Smallprogram.dao.impl;

import Smallprogram.dao.BaseDao;
import Smallprogram.dao.DirectoryDao;
import Smallprogram.entiy.Directory;
import Smallprogram.entiy.Snacksid;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DirectoryDaoimpl extends BaseDao implements DirectoryDao {
    @Override
    public List<Directory> LISTDirectory() {
        String sql = "select Directoryid,Directoryname from Directory";
        ResultSet res = this.executeQuery(sql);
        List<Directory> list = new ArrayList<>();
        try {
            while (res.next()) {
                list.add(new Directory(res.getInt(1), res.getString(2)));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<Directory> listName() {
        String sql = "SELECT nam.Directoryname,nam.Directoryid, snie.snacksld,snie.snacksName,snie.snacksPtion,snie.snacksPrice,snie.snacksFeature,snie.snackTime,snie.Shelf,snacksYhPrice,snie.Description,snie.BevegPrice,snie.Classification FROM `Directory` nam LEFT JOIN snacks snie ON snie.`Directory` = nam.`Directoryid` ";
        ResultSet res = this.executeQuery(sql);
        List<Directory> list = new ArrayList<>();
        List<Snacksid> listid = null;
        try {
            while (res.next()) {
                listid = new ArrayList<>();
                String name = "https://liualing.oss-cn-guangzhou.aliyuncs.com/" + res.getString(11) + res.getString(12);
                listid.add(new Snacksid(res.getInt(3), res.getString(4), res.getString(5), res.getDouble(6), res.getString(7), res.getString(8), res.getInt(9), res.getDouble(10), res.getInt(13), name));
                list.add(new Directory(res.getInt(2), res.getString(1), listid));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }



}
